//
//  JKHorizontalGrideView.m
//  JKHorizontalGrideView
//
//  Created by jiukuyinyue on 2017/8/18.
//  Copyright © 2017年 LC. All rights reserved.
//

#import "JKHorizontalGrideView.h"

@implementation JKHorizontalGrideViewCellConfig

IMPLEMENTATION_SINGLETON(JKHorizontalGrideViewCellConfig)

@end

@interface JKHorizontalGrideViewCell : UICollectionViewCell

@property (strong, nonatomic)   UILabel         *titleLabel;

@property (strong, nonatomic)   UIImageView     *iconImageView;

@property (assign, nonatomic)   NSInteger       defaultBottom;

@end

@implementation JKHorizontalGrideViewCell

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        _titleLabel = [UILabel new];
        _titleLabel.textAlignment = NSTextAlignmentCenter;
        _titleLabel.font = [UIFont systemFontOfSize:13];
        [self.contentView addSubview:_titleLabel];
        _iconImageView = [UIImageView new];
        [self.contentView addSubview:_iconImageView];
     
//        CGFloat width = [[UIScreen mainScreen] bounds].size.width / 3;
//        [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.left.right.equalTo(self.contentView);
//            make.bottom.equalTo(self.contentView.mas_bottom).offset(0);
//            make.height.mas_equalTo(20);
//        }];
//        [_iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.size.mas_equalTo(CGSizeMake(width, width));
//            make.centerX.equalTo(self.contentView);
//            make.bottom.equalTo(_titleLabel.mas_top);
//            make.top.equalTo(self.contentView.mas_top).offset(13);
//        }];
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    if ([JKHorizontalGrideViewCellConfig sharedInstance].cellConfigBlock != nil) {
        [JKHorizontalGrideViewCellConfig sharedInstance].cellConfigBlock(_titleLabel,_iconImageView,self);
    }
}

@end

@interface JKHorizontalGrideView () <UICollectionViewDataSource,UICollectionViewDelegate>

@property (strong, nonatomic)   JKHorizontalGrideCollectionViewLayout *layout;

@end

@implementation JKHorizontalGrideView

- (UICollectionView *)collectionView {
    if(_collectionView == nil) {
        self.layout = [JKHorizontalGrideCollectionViewLayout new];
        // 设置行数
        if([JKHorizontalGrideViewCellConfig sharedInstance].rowCount > 0) {
            self.layout.rowCount = [JKHorizontalGrideViewCellConfig sharedInstance].rowCount;
        } else {
            self.layout.rowCount = 2;
        }
    
        // 设置列数
        if([JKHorizontalGrideViewCellConfig sharedInstance].columnCount > 0) {
            self.layout.columnCount = [JKHorizontalGrideViewCellConfig sharedInstance].columnCount;
        } else {
            self.layout.columnCount = 4;
        }

        
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:self.layout];
        _collectionView.pagingEnabled = YES;
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.showsHorizontalScrollIndicator = NO;
        _collectionView.backgroundColor = [UIColor clearColor];
        [_collectionView registerClass:[JKHorizontalGrideViewCell class] forCellWithReuseIdentifier:@"CELL"];
        
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        [self addSubview:_collectionView];
        [_collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.right.equalTo(self);
            make.bottom.equalTo(self);//.pageControl.mas_top
        }];
    }
    return _collectionView;
}

/**
 刷新
 */
- (void)reloadData {
    [self.collectionView reloadData];
    
}

#pragma mark - UICollectionViewDataSource -
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    NSInteger count = 0;
    if(self.dataSource && [self.dataSource respondsToSelector:@selector(numberOfItemsInHorizontalGrideView:)]) {
        count = [self.dataSource numberOfItemsInHorizontalGrideView:self];
    }
    return count;
}

- (__kindof JKHorizontalGrideViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    JKHorizontalGrideViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CELL" forIndexPath:indexPath];
    NSString *title = @"";
    if(self.dataSource && [self.dataSource respondsToSelector:@selector(horizontalGrideView:titleForItemAtIndex:)]) {
        title = [self.dataSource horizontalGrideView:self titleForItemAtIndex:indexPath.row];
    }
    cell.titleLabel.text = title;
    
    if(self.dataSource && [self.dataSource respondsToSelector:@selector(horizontalGrideView:iconURLForItemAtIndex:)]) {
        
        NSString *url = [self.dataSource horizontalGrideView:self iconURLForItemAtIndex:indexPath.row];
        
        if(self.defaultImage) {
            cell.iconImageView.image = self.defaultImage;
        } else {
            cell.iconImageView.image = [UIImage imageNamed:url];
        }
    }
    return cell;
}


#pragma mark - UICollectionViewDelegate -
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if(self.delegate && [self.delegate respondsToSelector:@selector(horizontalGrideView:didSelectItemAtIndex:)]) {
        [self.delegate horizontalGrideView:self didSelectItemAtIndex:indexPath.row];
    }
}
@end

